using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Action.Dao;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Action.Service
{
    public class ActionPluginInfoService
    {
        private static readonly object lockObj = new object();
        private static ActionPluginInfoService _instance = null;
        private List<ActionPluginInfo> actionPlugins = null;
        public List<ActionPluginInfo> ActionPlugins
        {
            get
            {
                if(actionPlugins == null)
                    actionPlugins = new ActionPluginInfoDao().GetActionPluginInfo();
                return actionPlugins;
            }
        }
        private ActionPluginInfoService()
        {
        }
        public static ActionPluginInfoService Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObj)
                    {
                        if (_instance == null)
                            _instance = new ActionPluginInfoService();
                    }
                }
                return _instance;
            }
        }
        public ActionPluginInfo GetActionPluginInfoByActionCode(string actionCode)
        {
            var targetPlugin = ActionPlugins.Find(x=>x.ActionCode.Equals(actionCode,StringComparison.OrdinalIgnoreCase)&& x.IsEnable);
            if (targetPlugin == null)
                throw new FasException(ActionExecuteExceptionCode.LoadActionExecuteEngineError,string.Format(CommonStringClassService.ActionPluginInfoService_GetActionPluginInfoByActionCode,actionCode));
            return targetPlugin;
        }
    }
}
